package edu.hawaii.duedates.library;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import org.junit.Ignore;
import org.junit.Test;
import edu.hawaii.duedates.BorrowedItem;

/**
 * JUnit Test Class for HawaiiState class.
 * 
 * @author John Ly
 *
 */
public class TestHawaiiState {
  private final String id = System.getProperty("hsl.id");
  private final String password = System.getProperty("hsl.password");  
  private final HawaiiState hiLib = new HawaiiState();
  List<BorrowedItem> bookList = new ArrayList<BorrowedItem>();
  
  /**
   * Test convertDateToCalendar.
   * @throws Exception If date fails to parse. 
   */
  @Test
  public void testConvertDateToCalendar () throws Exception {
    Calendar cal = hiLib.convertDateToCalendar("11/15/2008");
    //Calendar.MONTH is zero-based. 0 is January, 1 is February, etc.
    assertEquals("Verify Calendar.MONTH", cal.get(Calendar.MONTH), 10);
    assertEquals("Verify Calendar.DAY_OF_MONTH", cal.get(Calendar.DAY_OF_MONTH), 15);
    assertEquals("Verify Calendar.YEAR", cal.get(Calendar.YEAR), 2008);
  }
  
  /**
   * Test getInfo (which also calls login) with account with books.
   * @throws Exception If error logging into Hawaii State Library.
   */
  @Ignore ("Hudson does not pass arguments") 
  @Test
  public void testGetInfo () throws Exception {
    assertTrue("Testing bookList empty", bookList.isEmpty());
    System.out.println("Retrieving information....");
    bookList = hiLib.getInfo(this.id, this.password, bookList);
    if (!bookList.isEmpty()) {
      assertFalse("Testing bookList not empty", bookList.isEmpty());
      assertTrue("Testing bookList has book", bookList.get(0) instanceof BorrowedItem);      
    }
  }
  
  /**
   * Test login method with fake account. The test should expect an Exception.
   * @throws Exception When error logging in.
   */
  @Ignore ("Test can take forever sometimes")
  @Test (expected = Exception.class)
  public void testIllegalLogin () throws Exception {
    hiLib.getInfo("", "", bookList);
    fail("Login with fake account shouldn't have passed");
  }
  

}
